"""
@author: xyl
@file: youxi.py
@time: 2022/12/2 11:56
"""
# 角色  房间
# 角色 -- 属性
# 名字、性别、血量、房间
# 战斗

# 房间  -- 属性
# 房间号，房间成员
# 加入房间

# 创建角色 创建房间  哪个房间开始战斗

import random
class Role(object):
    def __init__(self, name, sex, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.room = None
    def attach(self, other):
        fight = {11: "屠龙刀", 12: "倚天剑"}
        if self == other:
            print("不能攻击自己")
        else:
            if self.room and self.room == other.room:
                 if self.blood <= 0:
                    print("您的血量低于0，不能进行攻击")
                 else:
                    if random.choice([0, 1]) == 0:
                        lost_blood = random.choice([11, 12])
                        print(f"{other.name}没有使用护盾")
                    else:
                        lost_blood = random.choice([10, 20])
                        print(f"{other.name}使用了护盾")
                    other.blood = other.blood - lost_blood
                    print(f"{self.name}使用{fight[lost_blood]}攻击了{other.name}{lost_blood}点血量，当前血量{other.blood}")
                    if other.blood <= 0:
                        print(f"{other.name}已阵亡，不能再进行攻击")
                        exit()
            else:
                print("不在同一个房间不能攻击!")

class Room(object):
    def __init__(self, name):
        self.name = name
        self.member_list = []
    def add_member(self, role):
        if role.room == None:
            if len(self.member_list) < 2:
                self.member_list.append(role)
                role.room = self
                print(f"{role.name}成功加入{self.name}房间")
        else:
            print(f"{role.name}已加入{role.room.name}房间")

roles = {}
rooms = {}
while True:
    print(
        "1.创建角色\n",
        "2.创建房间\n",
        "3.加入房间\n",
        "4.开始战斗\n",
        "5.退出"
    )
    op = input("请输入您的操作：")
    if op.isdigit():
        op = int(op)
        if 1 <= op <= 5:
            if op == 1:
                name,sex = input("请输入角色的姓名与性别，空格分隔：\n").split()
                roles[name] = Role(name,sex)
            elif op == 2:
                room = input("请输入房间的名字：\n")
                rooms[room] = Room(room)
            elif op == 3:
                print(f"当前角色有{roles.keys()}，当前房间有{rooms.keys()}")
                role_name, room_name = input("请输入您要加入的角色和房间名，空格分隔:\n").split()
                rooms[room_name].add_member(roles[role_name])
            elif op == 4:
                while True:
                    rooms[room_name].member_list[0].attach(rooms[room_name].member_list[1])
                    rooms[room_name].member_list[1].attach(rooms[room_name].member_list[0])
            elif op == 5:
                break
        else:
            print("输入错误！请输入1-5选择操作！")
    else:
        print("输入有误！请重新输入！")